//
//  BGMyBuyListController.m
//  bigMall
//
//  Created by LTOVE on 16/9/2.
//  Copyright © 2016年 thinklev. All rights reserved.
//

#import "BGMyBuyListController.h"
#import "MJRefresh.h"
#import "BGDateResult.h"
#import "BGShellerOrderListModel.h"
#import "BGSellerOrderCell.h"

#import "BGBuyerListDetailController.h"

#import "MBProgressHUD+MJ.h"
#import "MBProgressHUD.h"

@interface BGMyBuyListController (){
    
    NSMutableArray *_dataArray;
    NSUInteger _thisPage;
    NSUInteger _allPage;
}


@end
static NSString *const reuseIdentify = @"cell";
@implementation BGMyBuyListController

- (void)setType:(NSString *)type
{
    _type = type;
    [self.tableView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    [self.tableView.header beginRefreshing];
    
    
    
    
    
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}


- (void)loadData
{
    [MBProgressHUD showMessage:@"加载中..."];
    [BGDateResult buyerOrderListWithThisPage:@"1" pageNum:@"10" type:self.type success:^(NSObject *result) {
        [MBProgressHUD hideHUD];
        BGShellerOrderListModel *resultModel = (BGShellerOrderListModel *)result;
        _dataArray = [NSMutableArray arrayWithArray:resultModel.list];
        
        
        _thisPage = 2;
        _allPage = [resultModel.allPage integerValue];
        [self.tableView.header endRefreshing];
        [self.tableView reloadData];
        if (_dataArray.count >= 10) {
            self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
        }
        
    } failure:^(NSError *error) {
        
        [MBProgressHUD hideHUD];
    }];
}
- (void)loadMoreData
{
    if (_thisPage > _allPage) {
        [self.tableView.footer endRefreshingWithNoMoreData];
        return;
    }
    [MBProgressHUD showMessage:@"加载中..."];
    [BGDateResult buyerOrderListWithThisPage:[NSString stringWithFormat:@"%lu",(unsigned long)_thisPage] pageNum:@"10" type:self.type success:^(NSObject *result) {
        [MBProgressHUD hideHUD];
        BGShellerOrderListModel *resultModel = (BGShellerOrderListModel *)result;
        [self.tableView.footer endRefreshing];
        [_dataArray addObjectsFromArray:resultModel.list];
        [self.tableView reloadData];
        if (resultModel.list.count) {
            _thisPage ++;
        }
        
    } failure:^(NSError *error) {
        
        [MBProgressHUD hideHUD];
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    return _dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 197;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BGSellerOrderCell *cell = [BGSellerOrderCell cellWithTableView:tableView andIndexPath:indexPath];
    BGShellerOrderListListAddayModel *model = _dataArray[indexPath.row];
    cell.model = model;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    BGBuyerListDetailController *vc = [[BGBuyerListDetailController alloc]init];
//    UIBarButtonItem *bacItem = [[UIBarButtonItem alloc]init];
//    bacItem.title = @"";
//    self.navigationItem.backBarButtonItem = bacItem;
    BGShellerOrderListListAddayModel *model = _dataArray[indexPath.row];
    vc.orderId = model.idStr;
    vc.strs = model.pay_status;
    vc.title = @"订单详情";
    UIBarButtonItem *bac = [[UIBarButtonItem alloc]init];
    bac.title = @"";
    vc.navigationItem.backBarButtonItem = bac;
//    [vc.navigationItem.backBarButtonItem setTitle:@"sss"];
    
    [self.navigationController pushViewController:vc animated:YES];
}


@end
